<?php

namespace App\Http\Controllers;

use App\Http\Requests\PasswordResetRequest;
use App\Http\Requests\UserRewardRequest;
use App\Models\User;
use Illuminate\Support\Facades\Hash;

class UserPasswordController extends Controller
{
    /**
     * Show the form for editing the specified resource.
     *
     * @param User $user
     * @return \Illuminate\Http\Response
     */
    public function edit(User $user)
    {
        //dd(Hash::check('111111', $user->password));
        return view('resources.user.password.edit')->with('user', $user);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \App\Http\Requests\PasswordResetRequest $request
     * @param User $user
     * @return \Illuminate\Http\Response
     */
    public function update(PasswordResetRequest $request, User $user)
    {

        if (Hash::check($request->previous_password, $user->password)) {
            $user->password = bcrypt($request->password);
            $user->save();

            return redirect()->route('users.show', ['user' => $user]);
        } else {
            return redirect()->back()->withErrors(['原密码不正确']);
        }
    }
}
